package com.amado.course20Tree;

import javax.sound.midi.Soundbank;

/**
 * Created by Administrator on 2018/6/20.
 */
public class NodeService2 {
	/**
	 * 一级菜单
	 */
	private Node[] nodeArray;

	NodeService2() {
		init();
		Node node1 = new Node("节点1", "节点1");
		Node node1_1 = new Node("节点1_1", "节点1_1");
		Node node1_2 = new Node("节点1_2", "节点1_2");

		Node node1_1_1 = new Node("节点1_1_1", "节点1_1_1");

		node1.setChildNodes(new Node[] { node1_1, node1_2 });

		node1_1.setChildNodes(new Node[] { node1_1_1 });

		Node node2 = new Node("节点2", "节点2");
		Node node2_1 = new Node("节点2_1", "节点2_1");
		node2.setChildNodes(new Node[] { node2_1 });

		Node node3 = new Node("节点3", "节点3");

		nodeArray = new Node[] { node1, node2, node3 };

	}

	void init() {

	}

	/**
	 * 添加节点
	 * 
	 * @param parentNodeCode 父节点CODE
	 * @param nodeName       节点名称
	 */
	public void addNode(String parentNodeCode, String nodeName) {

	}

	/**
	 * 删除节点
	 * 
	 * @param nodeName 节点code
	 * @return 成功返回true,否则返回false
	 */
	public boolean deleteNode(String nodeName) {
		return true;
	}

	/**
	 * 修改节点
	 * 
	 * @param nodeCode 节点编号
	 * @param nodeName 节点名称
	 */
	public void updateNode(String nodeCode, String nodeName) {

	}

	/**
	 * 打印所有子节点
	 */
	public void printNode() {
	}

	/**
	 * 打印节点
	 * 
	 * @param nodeArray 菜单数组
	 * @param level     菜单级别
	 */
	public void printNode(Node[] nodeArray, int level) {
	}

	public void printNode(Node[] nodeArray) {
		for (Node node : nodeArray) {
			System.out.println(node.getNodeCode());
			if (node.getChildNodes() == null) {
				continue;
			}
			for (Node tempNode : node.getChildNodes()) {
				System.out.println(tempNode.getNodeCode());
				if (tempNode.getChildNodes() == null) {
					continue;
				}
				for (Node tempNode2 : tempNode.getChildNodes()) {
					System.out.println(tempNode2.getNodeCode());
				}
			}
		}
	}

	public void printNodeTwo(Node[] nodeArray) {
		if (nodeArray == null) {
			return;
		}
		//nodeArray node1-1 node1-1-1
		for (Node node : nodeArray) {
			//node1  node1-1 node1-1-1   node1-2  node2 node2-1 node3
			System.out.println(node.getNodeCode());
			//node1-1
			printNodeTwo(node.getChildNodes());
		}
	}

	/**
	 * 根据级别打印空格
	 * 
	 * @param level 菜单级别
	 */
	private void printBlank(int level) {
		for (int i = 0; i < level; i++) {
			System.out.print("   ");
		}
	}

	/**
	 * 打当nodeCode下的所有子节点
	 */
	public void printNode(String nodeCode) {

	}

	public static void main(String[] args) {
		NodeService2 sevice = new NodeService2();
		sevice.printNodeTwo(sevice.nodeArray);
	}
}
